﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.ObjectModel;
using Frogism.Core;
using Frogism.Domain;
using Frogism.Web.Models;

namespace Frogism.Web.Controllers
{
    public class BookController : FrogControllerBase
    {
      private IBookManager bookManager;
      public BookController(IBookManager bookManager) {
        this.bookManager = bookManager;
      }
      public BookController() {
        this.bookManager = ManagerService.GetBookManager();
      }
      public ViewResult Finished() {
          yearstats();
        return View("Index",new BookViewData(bookManager.GetAll().Where(b=>b.IsFinished).ToCollection()));
      }
      public ViewResult NotFinished() {
          yearstats();
        return View("Index", new BookViewData(bookManager.GetAll().Where(b => !b.IsFinished).ToCollection()));
      }
      public ActionResult BookNew() {
        IBook book = new Book();
        book.Id = -1;
        ViewData["AuthorId"] = new SelectList(ManagerService.GetAuthorManager().GetAll(), "Id", "Name");
        return View("BookEdit", book);
      }
      public ActionResult Summary(int id)
      {
          var book = bookManager.Get(id);
          var subBook = new {Title2 = book.Title};
          return Json(subBook);
      }
      public ActionResult BookEdit(int id) {
        IBook book = bookManager.Get(id);
        ViewData["AuthorId"] = new SelectList(ManagerService.GetAuthorManager().GetAll(), "Id", "Name", book.Author.Id);
        return View("BookEdit", book);
      }
      public ActionResult Index()
        {
          return RedirectToAction("Current");
        }
        public ActionResult All(int? id) {
          // Add action logic here
            yearstats(id??DateTime.Now.Year);

            ViewData["YearList"] = YearList();



          if (id == null) {
            return View("Index", new BookViewData(bookManager.GetAll()));
          } else {
            return View("Index", new BookViewData(bookManager.GetAll().Where(b => b.GoalYear == id || id == b.LastYearRead).ToCollection()));
          }
        }

        private IList<int> YearList()
        {
            var list = new List<int>();
            int temp = 2003;
            int gotoYear = DateTime.Now.Year+1;
            while (temp <= gotoYear)
            {
                list.Add(temp++);
                
            }
            return list;
        }
        public ActionResult ReadUpdate(int id, int BookId, DateTime ReadDate, int Page)
        {
          IBook book = bookManager.Get(BookId);

            IRead read = new Read();
            read.Id = id;
            read.Page = (int)Math.Floor(Page/book.Factor);
            read.ReadDate = ReadDate;

            bookManager.UpdateRead(book, read);
            return RedirectToAction("Index");
        }
        public ActionResult ReadDelete(int id) {
          IRead r = new Read(){Id = id};
          bookManager.DeleteRead(r);
          return RedirectToAction("Index");
        }
        public ActionResult Read(int id, int BookId)
        {

          IBook book = bookManager.Get(BookId);
          IRead read = bookManager.GetReadById(book, id);
            ViewData["Book"] = book;
            return View("ReadEdit", read);
        }
        public ActionResult UpdateToday(int id, string title)
        {

          IBook book = bookManager.Get(id);
          IRead read = bookManager.GetOrCreateTodaysRead(book);
            ViewData["Book"] = book;
            return View("ReadEditBasic", read);
        }
        public ActionResult BookUpdate(int id, int AuthorId, string Title, int LastPageNumber, int? IndexLength,double? Factor, int GoalYear, bool IsPrivate, bool IsFinished) {
          IBook b;
          if (id == -1) {
            b = new Book();

          }else{
            b = bookManager.Get(id);
          }
          b.Author = ManagerService.GetAuthorManager().Get(AuthorId);
          b.Title = Title;
          b.LastPageNumber = LastPageNumber;
          b.IndexLength = IndexLength??0;
          b.Factor = Factor??0;
          b.GoalYear = GoalYear;
          b.IsPrivate = IsPrivate;
          b.IsFinished = IsFinished;

          bookManager.Update(b);

          return RedirectToAction("Index");
        }
        public ActionResult ReadsByBook(int id, string title)
        {
          return RedirectToAction("BookEdit",new { id=id, title = title});
          //IBook book = bookManager.Get(id);
          //  ViewData["BookId"] = id;
          //  return View("ReadList", book.Reads);
        }
        public ViewResult Current()
        {
          yearstats(DateTime.Now.Year);
          return View("Index", new Models.BookViewData(bookManager.GetReading()));
        }
        private void yearstats()
        {
            yearstats(DateTime.Now.Year);
        }
        private void yearstats(int year)
        {
            var books = bookManager.GetAll();
            ViewData["YearStats"] = (new Core.ReportPerYear(books)).GetStats();
            ViewData["MonthStats"] = (new Core.ReportPerMonth(books)).GetStats(year);
            ViewData["MonthStatsYear"] = year;

        }
       
      
    }
}
